﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Hosting;
using System.IO;
using System.Web.UI;
using System.Data;
using System.Reflection;
using XDSoft.Model;
using System.ComponentModel;

namespace XDSoft.Web
{
    /// <summary>
    /// 站点枚举
    /// </summary>
    public static class EnumCommon
    {
        /// <summary>
        /// 通用分类类型定义
        /// </summary>
        public enum OptionsType
        {
            /// <summary>
            /// 商标类型
            /// </summary>
            [Description("商标类型")]
            BrandType,
            /// <summary>
            /// 商标状态
            /// </summary>
            [Description("商标状态")]
            BrandStatus,
            /// <summary>
            /// 客户等级
            /// </summary>
            [Description("客户等级")]
            ClientLevel,
            /// <summary>
            /// 国际分类号
            /// </summary>
            [Description("国际分类号")]
            InternationalCode

        }
    }

    public class WebCommon
    {
        /// <summary>
        /// 获取枚举描述信息
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static string GetEnumDescription(Enum en)
        {
            Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return en.ToString();
        }


        /// <summary>
        /// DataTable替换DataField字段名
        /// </summary>
        /// <param name="t"></param>
        /// <param name="dt"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static DataTable ChangeDataTableFieldAttrName(Type t,DataTable dt,string tableName)
        {
            PropertyInfo[] infos = t.GetProperties();
            DataSet newDs = new DataSet();
            DataTable newDT = new DataTable(tableName);
            newDT = dt.Copy();
            foreach (PropertyInfo info in infos)
            {
                object[] objs = info.GetCustomAttributes(typeof(DataFieldAttribute), false);
                if (objs != null)
                    newDT.Columns[info.Name].ColumnName = ((DataFieldAttribute)objs[0]).FieldName;
            }
            return newDT;
        }

        /// <summary>
        /// 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="specifiedPath">绝对路径</param>
        /// <returns>虚拟路径, 型如: ../../</returns>
        public static string ConvertSpecifiedPathToRelativePathForPage(Page page, string specifiedPath)
        {
            // 根目录虚拟路径
            string virtualPath = page.Request.ApplicationPath;
            // 根目录绝对路径
            string pathRooted = HostingEnvironment.MapPath(virtualPath);
            // 页面虚拟路径
            string pageVirtualPath = page.Request.Path;
            if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
            {
                throw new Exception(string.Format("[{0}]是虚拟路径而不是绝对路径!", specifiedPath));
            }
            // 转换成相对路径 
            //(测试发现，pathRooted 在 VS2005 自带的服务器跟在IIS下根目录或者虚拟目录运行似乎不一样,
            // 有此地方后面会加"/", 有些则不会, 为保险起见判断一下)
            if (pathRooted.Substring(pathRooted.Length - 1, 1) == "//")
            {
                specifiedPath = specifiedPath.Replace(pathRooted, "/");
            }
            else
            {
                specifiedPath = specifiedPath.Replace(pathRooted, "");
            }
            string relativePath = specifiedPath.Replace("//", "/");
            string[] pageNodes = pageVirtualPath.Split('/');
            // 减去最后一个页面和前面一个 "" 值
            int pageNodesCount = pageNodes.Length - 2;
            for (int i = 0; i < pageNodesCount; i++)
            {
                relativePath = "/.." + relativePath;
            }
            if (pageNodesCount > 0)
            {
                // 如果存在 ".." , 则把最前面的 "/" 去掉
                relativePath = relativePath.Substring(1, relativePath.Length - 1);
            }
            return relativePath;
        }

        /// <summary>
        /// 第二个方法显然是从第一个方法中的前部分抽取出来的,所以懒得去添加相关注释 :P将Web站点下的绝对路径转换为虚拟路径
        /// 将Web站点下的绝对路径转换为虚拟路径
        /// 注：非Web站点下的则不转换
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="specifiedPath">绝对路径</param>
        /// <returns>虚拟路径, 型如: ~/</returns>
        public static string ConvertSpecifiedPathToRelativePath(Page page, string specifiedPath)
        {
            string virtualPath = page.Request.ApplicationPath;
            string pathRooted = HostingEnvironment.MapPath(virtualPath);
            if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
            {
                return specifiedPath;
            }
            if (pathRooted.Substring(pathRooted.Length - 1, 1) == "//")
            {
                specifiedPath = specifiedPath.Replace(pathRooted, "~/");
            }
            else
            {
                specifiedPath = specifiedPath.Replace(pathRooted, "~");
            }
            string relativePath = specifiedPath.Replace("//", "/");
            return relativePath;
        }

        /// <summary>
        /// 获取网站虚拟路径目录部分
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string GetVPath(Page page)
        {
            string vPath = page.Request.Url.GetLeftPart(System.UriPartial.Authority) + page.Request.ApplicationPath;
            if (!vPath.EndsWith("/"))
                vPath = vPath + "/";
            return vPath;
        }

        /// <summary>
        /// 获取当前页面路径(不带页面所在目录、虚拟目录)
        /// </summary>
        /// <returns></returns>
        public static string GetPageUrl()
        {
            string url = HttpContext.Current.Request.Url.PathAndQuery.ToString();
            int tag = url.LastIndexOf("/") + 1;
            return url.Substring(tag);
        }

    }
}
